﻿using System;
using System.Web;
using System.Web.Http;
using System.Net.Http;
using System.Text;
using Aop.Api.Util;
using System.Collections.Generic;
using System.Collections.Specialized;
using YiYiTong.Rocket.Business;
using YiYiTong.Rocket.Model;
using YiYiTong.Rocket.Helper;

namespace YiYiTong.Rocket.Web.Controllers
{
    public class AlipayController : ApiController
    {
        /// <summary>
        /// 支付宝支付结果通知
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public HttpResponseMessage Notify()
        {
            Dictionary<string, string> sArray = GetRequestPost();
            LogHelper.Info("支付宝支付结果通知" + JsonHelper.Serialize(sArray));
            if (sArray.Count != 0)
            {
                string publicKey = ConfigHelper.AppSetting<string>("zfb_publickey");
                bool flag = AlipaySignature.RSACheckV1(sArray, publicKey, "UTF-8", "RSA2", false);
                if (flag)
                {
                    string tradeStatus = string.Empty;
                    if (sArray.ContainsKey("trade_status"))
                        tradeStatus = sArray["trade_status"];

                    if (!string.IsNullOrEmpty(tradeStatus) && tradeStatus.Equals("TRADE_SUCCESS"))
                    {
                        string orderId = sArray["out_trade_no"];    //系统订单号
                        string tradeNo = sArray["trade_no"];        //支付宝订单号
                        decimal amt = ConvertHelper.ConvertTo<decimal>(sArray["total_amount"]);

                        long order_master_id = 0;  //支付订单id
                        int business_order_type = 0;//1 销售订单 2竞价报名订单
                        //支付成功，开始处理业务逻辑
                        if (OrderBiz.Instance.ChargeOrderSucc(orderId, tradeNo, amt, out order_master_id,out business_order_type))
                        {
                            if (order_master_id > 0)
                            {
                                if (business_order_type == (int)BusinessOrderType.SaleOrder)
                                {
                                    //销售订单支付订单付款
                                    if (!OrderBiz.Instance.OrderPaySucc(order_master_id, (int)PayType.Alipay))
                                        return GetReturn("fail4");
                                }
                                else if (business_order_type == (int)BusinessOrderType.BaomingOrder)
                                {
                                    //竞价报名订单付款
                                    if (!JingjiaBiz.Instance.BaomingPaySucc(order_master_id, (int)PayType.Alipay))
                                        return GetReturn("fail4");
                                }
                            }
                            return GetReturn("success");
                        }
                        else
                            return GetReturn("fail4");
                    }
                    else
                        return GetReturn("fail1");
                }
                else
                    return GetReturn("fail2");
            }
            else
                return GetReturn("fail3");
        }

        private HttpResponseMessage GetReturn(string str)
        {
            HttpResponseMessage resp = new HttpResponseMessage { Content = new StringContent(str, Encoding.GetEncoding("UTF-8"), "text/plain") };
            return resp;
        }

        private Dictionary<string, string> GetRequestPost()
        {
            HttpContextBase context = (HttpContextBase)Request.Properties["MS_HttpContext"];//获取传统context
            HttpRequestBase request = context.Request;//定义传统request对象

            NameValueCollection coll;
            Dictionary<string, string> sArray = new Dictionary<string, string>();
            coll = request.Form;

            String[] requestItem = coll.AllKeys;
            for (int i = 0; i < requestItem.Length; i++)
            {
                sArray.Add(requestItem[i], request.Form[requestItem[i]]);
            }
            return sArray;
        }
    }
}
